/**
 * This code is part of the Bumpslide Library by David Knape
 * http://bumpslide.com/
 * 
 * Copyright (c) 2006, 2007, 2008 by Bumpslide, Inc.
 * 
 * Released under the open-source MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * see LICENSE.txt for full license terms
 */ 
package {
	import flash.display.Shape;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import com.bumpslide.tween.FTween;
	import com.bumpslide.tween.FTweenOptions;
	import com.bumpslide.ui.Applet;
	import com.bumpslide.ui.FramerateMonitor;		

	/**
	 * FTween Options Demo
	 * 
	 * @mxmlc -sp=../../classes/
	 * @author David Knape
	 */
	[SWF(backgroundColor="0xffffff",frameRate="31")]
	public class FTweenOptionsDemo extends Applet {

		 
		static private const NUM_BOXES:int=500;
		static private const TWEEN_DELAY:int = 10;
		
		private var loopTimer:Timer;
		private var boxes:Array = new Array();
		
		override protected function addChildren():void {
            
            loopTimer = new Timer( NUM_BOXES*TWEEN_DELAY, 0);
            loopTimer.addEventListener(TimerEvent.TIMER, loop);
			loopTimer.start();
            
            var shp:Shape;
            
            // create boxes
            for(var n:int=0; n<NUM_BOXES; ++n) {
            	shp = new Shape();
            	shp.graphics.beginFill( 0, .2 + Math.random() * .8 );
            	shp.graphics.drawRect( 0, 0, 4, 4 );
            	addChild( shp );
            	boxes.push( shp );
            }       
                        
			addChild( new FramerateMonitor() );
			
			loop();
		}        
                                           
		/**
		 * Update the target box position to match the mouse position
		 */
		private function loop( e:TimerEvent=null ):void {
			
			var n:int = NUM_BOXES;
			
			var options:FTweenOptions = new FTweenOptions();
			
			// minDelta - how close we need to be to the target to declare us finished tweening
			options.minDelta = .1;
			
			// updateDelay - how many milliseconds between updates
			// default is 30, increasing to 50 gives us a bit more performance 
			// at the expense of smoothness
			options.updateDelay = 50;			
			
			while( n-- ) {				
				// milliseconds delay
				options.delay = TWEEN_DELAY*n;
				
				FTween.ease(boxes[n], 'x', Math.random()*width, .15, options );
				FTween.ease(boxes[n], 'y', Math.random()*height, .3, options );
			}
		}

	}
}